<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\index\controller;

use think\facade\View;
use think\facade\Filesystem;
use app\index\BaseController;
use app\index\model\Config;
use app\index\addons\User;
use plugins\cms\index\model\Job as JobModel;
/**
 * 招聘
 */
class Job extends BaseController
{
    /**
     * 列表
     */
    public function list()
    {
        $config = Config::getVal('app_cms');
        $input = input();
        $prop = empty($input['prop']) ? '' : $input['prop'];
        $order = empty($input['order']) ? '' : $input['order'];
        $order = ! empty($prop) && ! empty($order) ? [$prop => $order] : ['istop' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];
        $where[] = ['status', '=', 1];
        $where[] = ['catalog_id', 'find in set', $this->request->catalog['id']];
        $pageSize = isset($config['job_page']) ? $config['job_page'] : 10;
        $append = ['url','catalog_title','resume_count'];
        $list   = JobModel::withSearch(['keyword'], $input)->where($where)->order($order)->append($append)->paginate($pageSize);
        View::assign([
            'list' => $list,
            'page' => $list->render()
        ]);
        return View::fetch("list/job");
    }

    /**
     * 详情
     */
    public function single()
    {
        $where[] = ['status', '=', 1];
        $where[] = ['id|seo_url', '=', input('param')];
        $append = ['catalog_title','resume_count','relation_list'];
        $single = JobModel::where($where)->append($append)->find();
        // 不存在404
        if (!$single) return abort(404);
        // 点击数+1
        $single->clicks = $single->clicks + 1;
        $single->save();
        // 上下条
        $field = 'id,catalog_id,title,seo_url';
        $npWhere[] = ['catalog_id', 'find in set', request()->catalog['id']];
        $npWhere[] = ['status', '=', 1];
        $single->prev = JobModel::where('id','>',$single->id)->field($field)->where($npWhere)->order('id', 'asc')->find();
        $single->next = JobModel::where('id','<',$single->id)->field($field)->where($npWhere)->order('id', 'desc')->find();
        View::assign([
            'single'          => $single,
            'seo_title'       => empty($single->seo_title) ? $this->request->catalog['seo_title'] : $single->seo_title,
            'seo_keywords'    => empty($single->seo_keywords) ? $this->request->catalog['seo_keywords'] : $single->seo_keywords,
            'seo_description' => empty($single->seo_description) ? $this->request->catalog['seo_description'] : $single->seo_description,
        ]);
        return View::fetch("single/job");
    }
}